<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/11/24
 * Time: 15:51
 */

namespace App\Plugin\HostServer\Admin;



use App\Plugin\HostServer\Models\PnHostSvCategory;
use App\Plugin\HostServer\Models\PnHostSvGood;
use App\Plugin\HostServer\Models\PnHostSvSystemGoods;

class GoodsController extends BaseController
{

    public function indexData()
    {
        $item = [];
        $item['navs'] =PnHostSvCategory::where('parent_id',0)->where('group_type','server')->get()->toArray();
        $item['on_id']=$item['navs'][0]['id']??0;
        if($on_id=request()->input('category_id')){
            $item['on_id']=$on_id;
        }

        return $item;
    }
    public function setModelAddRelaction($model)
    {
        return $model->with('category','level'); // TODO: Change the autogenerated stub
    }

    public function apiJsonItemExtend($item)
    {
        $item->category_name=$item->category['name']??'';
        $item->level_name=$item->level['name']??'';
        return $item;
    }
    public function setModel()
    {
        $this->model= new PnHostSvGood();
    }

    public function afterSave($model, $id = '')
    {
        PnHostSvSystemGoods::add($this->rq->input('rel'),$model->id);
        //这里添加产品支持得服务器的id数组
        $list=$this->rq->input('rel');
        $catagrey=array_keys($list);
        $this->addGoodSystem($catagrey,$model->id);
    }


    /**
     * 创建者:蒙牛、阿磊
     * 创建时间:2019/12/9 21:30
     * 函数说明:根据产品id更新 support_system 字段为所支持的服务器类型id数组
     * 调用方式:get
     * @param $data
     * @param $id
     * @return mixed
     */
    public function addGoodSystem($data,$id)
    {
        $listArr=PnHostSvGood::where('id',$id)->update([
            'support_system'=>$data
        ]);
        return $listArr;
    }

    /**
     * 系统添加
     * @param $data
     * @param $goods_id
     * @return array|bool
     */
    public function createEditData($show = '')
    {
        $pid=request()->input('category_id');
        $where=[
            'path_level'=>[
                'type'=>'right_like',
                'value'=>$pid.'-'
            ]
        ];
        $server=PnHostSvCategory::category('server',$where,$pid);
        $system=PnHostSvCategory::category('system');
        $select_ids=[];
        $category_ids=[];
        if($show){
            $select_ids=$show->systems()->pluck('sub_id')->toArray();
            $category_ids=$show->systems()->pluck('category_id')->toArray();
        }
        $system=$this->treeData($system,$category_ids,$select_ids);
        //dump($system);
        return [
            'server'=>$server,
            'system'=>$system,
            'category_id'=>$pid
        ];
    }
    public function treeData($data, $category_ids,$select_ids)
    {

        $permissions = $this->tree($data);
        foreach ($permissions as $key1 => $item1) {
            $permissions[$key1]['own'] = in_array($item1['id'],$category_ids) ? 'checked' : false ;
            if (isset($item1['_child'])) {
                foreach ($item1['_child'] as $key2 => $item2) {
                    $permissions[$key1]['_child'][$key2]['own'] =in_array($item2['id'],$select_ids) ? 'checked'  : false;
                }
            }

        }
        return $permissions;


    }
}
